package com.maxd.service.helpTask;

import com.maxd.model.HelpClassify;
import com.maxd.model.HelpLuckyValue;
import com.maxd.respository.helpTask.HelpClassifyJpaRepository;
import com.maxd.respository.helpTask.HelpClassifyRepository;
import com.maxd.respository.helpTask.HelpLuckyValueJpaRepository;
import com.maxd.respository.helpTask.HelpLuckyValueRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 幸运值
 */
@Service
public class HelpLuckyValueServiceImpl implements IHelpLuckyValueService {

    private final HelpLuckyValueJpaRepository helpLuckyValueJpaRepository;
    private final HelpLuckyValueRepository helpLuckyValueRepository;

    @Autowired
    public HelpLuckyValueServiceImpl(HelpLuckyValueJpaRepository helpLuckyValueJpaRepository, HelpLuckyValueRepository helpLuckyValueRepository) {
        this.helpLuckyValueJpaRepository = helpLuckyValueJpaRepository;
        this.helpLuckyValueRepository = helpLuckyValueRepository;
    }


    @Override
    public HelpLuckyValue saveBody(HelpLuckyValue helpLuckyValue){
        return helpLuckyValueJpaRepository.save(helpLuckyValue);
    }

    @Override
    public HelpLuckyValue selectByUserId(Long userId){
        HelpLuckyValue helpLuckyValue = helpLuckyValueRepository.selectByUserId(userId);
        if(helpLuckyValue==null){
            helpLuckyValue=new HelpLuckyValue();
            helpLuckyValue.setLuckyValue(0.00);
            helpLuckyValue.setUserId(userId);
            helpLuckyValue.setAccruingAmounts(0.00);
            saveBody(helpLuckyValue);
        }
        return helpLuckyValue;
    }


    @Override
    public int updateHelpLuckyValue(Long userId,Double luckyValue){
        return helpLuckyValueRepository.updateHelpLuckyValue(userId,luckyValue);
    }

    @Override
    public Page<Map<String, Object>> selectLuckyQueueRanking(Pageable pageable, Long luckyQueueId, String phone) {
        if (StringUtils.isNotBlank(phone)) {
            return helpLuckyValueRepository.selectLuckyQueueRanking(pageable,luckyQueueId,phone);
        }
        return helpLuckyValueRepository.selectLuckyQueueRanking(pageable,luckyQueueId);
    }


}
